package org.jeecg.modules.wechat.job;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.http.HttpClient;
import org.jeecg.modules.wechat.vo.AccessTokenVO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

/**
 * 获取动态 AccessToken
 * Created by ：zou
 * Date：2019/8/22 17:54
 */
@Slf4j
@Data
public class AccessTokenJob implements Job {

    private static String WechatUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s";
    /**
     * 微信接口调用 参加的唯一标识
     */
    private static String accessToken;

    @Value("${WeChat.appID}")
    private  String appId;
    @Value("${WeChat.appsecret}")
    private  String secret;
    private static Long timerShaft=7200000L;


    /**
     * 每二个小时取一次token
     *
     * @return
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        WechatUrl=String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s", "client_credential", appId, secret);

        String json =null;
        try {
            json=HttpClient.sendGetData(WechatUrl, "utf-8");
            log.info( "获取公众号:"+ json );
        } catch (IOException e) {
            e.printStackTrace();
        }


        AccessTokenVO vo=JSON.parseObject(json, AccessTokenVO.class);

        accessToken = vo.getAccess_token();
}




}
